package com.shd.edanpei.presenter;

import android.text.TextUtils;

import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.network.api.Api;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.exception.ExceptionHandler;
import com.shd.edanpei.base.network.exception.ResponeThrowable;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.TeamModel;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.presenter.contract.TeamContract;
import com.shd.edanpei.util.ToastUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/11/20.
 * yang-wen-wen
 */

public class TeamPresenter extends BasePresenterImpl<TeamContract.TeamIView>
        implements TeamContract.TeamIPresenter {

    public TeamPresenter(TeamContract.TeamIView mView) {
        super(mView);
    }

    @Override
    public void queryTeamInfo() {
        String teamCode = DaoManager.getInstance().getUserBean().getTeamCode();
        if (TextUtils.isEmpty(teamCode)) {
            //未加入团队
            mBaseIView.noTeam();
        } else {
            mBaseIView.showLoading("");
            ApiCreator.getApi().queryMyTeam()
                    .compose(RxTransformer.<TeamModel>errorHandle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<TeamModel>() {
                        @Override
                        protected void onSuccess(TeamModel data) {
                            if (data.getTeamCode() != null){
                                //已加入团队
                                mBaseIView.haveTeam(data);
                            } else{
                                //未加入团队
                                mBaseIView.noTeam();
                                //清除团队码
                                DaoManager.getInstance().getUserBean().setTeamCode("");
                                DaoManager.getInstance().setTeamName("");
                            }
                        }


                        @Override
                        public void onFinish() {
                            mBaseIView.stopLoading();
                        }
                    });

        }
    }

    @Override
    public void joinTeam(String code) {
        mBaseIView.showLoading("");
        ApiCreator.getApi().joinTeam(code)
                .compose(RxTransformer.<TeamModel>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<TeamModel>() {
                    @Override
                    protected void onSuccess(TeamModel data) {
                        //团队码写入数据库
                        DaoManager.getInstance().setTeamCode(data.getTeamCode());
                        DaoManager.getInstance().setTeamName(data.getTeamName());
                        mBaseIView.haveTeam(data);
                    }

                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        ToastUtil.showToast(throwable.getMessage());
                    }


                    @Override
                    public void onFinish() {
                        mBaseIView.stopLoading();
                    }
                });
    }

    @Override
    public void exitTeam(final int state) {
        mBaseIView.showLoading("");
        ApiCreator.getApi().exitTeam(state)
                .compose(RxTransformer.<NoDataResponse>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NoDataResponse>() {
                    @Override
                    protected void onSuccess(NoDataResponse data) {
                        //清空数据口团队码
//                        DaoManager.getInstance().setTeamCode("");
//                        ToastUtil.showCenterToast("退团成功");
//                        mBaseIView.noTeam();
                        switch (state) {
                            case 1:
                                mBaseIView.cancelExit();
                                break;
                            case 2:
                                mBaseIView.clickExit();
                                break;
                        }
                    }

                    @Override
                    public void onFinish() {
                        mBaseIView.stopLoading();
                    }
                });

    }
}
